package com.resou.reader.reader.view;

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.utils.TextUtils;

/**
 * 支付章节布局
 */
public class PayChapterView extends LinearLayout {
    View viewLine; //横线
    TextView price_text_title;
    TextView balance_title;
    TextView tvSPay;
    TextView price_text;//价格
    TextView balance;//余额
    TextView tv_really_price;//实付金额
    TextView tvMonthlySub;//实付金额
    TextView tv_ResouForMe_pay;//热搜为我买单按钮
    TextView tvYPrice;//原价
    TextView tvDes;//描述
    RelativeLayout llContent;//容器
    LinearLayout llShowContent;//容器
    TextView tv_energy_deduction;//能量抵扣描述
    LinearLayout ll_really_price_container;
    LinearLayout llAutoSb;//自动购买点击区域
    ImageView ivAutoSb; //自动购买的显示图标
    TextView tv_chapter_pay; //购买按钮
    TextView tv_chapter_batch_pay;//购买更多章节

    boolean isAutoPay = false;//是否自动购买
    boolean isCliable = true;//是否可以点击
    boolean isAllBookPay = false;//是否是全本购买

    public PayChapterView(Context context) {
        super(context);
        initView();
    }

    public PayChapterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.chapter_pay_view, this);
        viewLine = view.findViewById(R.id.viewLine);
        price_text_title = view.findViewById(R.id.price_text_title);
        balance = view.findViewById(R.id.balance);
        tvSPay = view.findViewById(R.id.tvSPay);
        balance_title = view.findViewById(R.id.balance_title);
        price_text = view.findViewById(R.id.price_text);
        tvMonthlySub = view.findViewById(R.id.tvMonthlySub);
        TextUtils.setUnderLineSpan(tvMonthlySub, tvMonthlySub.getText().toString());
        tvDes = view.findViewById(R.id.tvDes);
        tv_ResouForMe_pay = view.findViewById(R.id.tv_ResouForMe_pay);
        tvYPrice = view.findViewById(R.id.tvYPrice);
        tv_really_price = view.findViewById(R.id.tv_really_price);
        llShowContent = view.findViewById(R.id.llShowContent);
        ivAutoSb = view.findViewById(R.id.ivAutoSb);
        llContent = view.findViewById(R.id.llContent);
        ll_really_price_container = view.findViewById(R.id.ll_really_price_container);
        tv_energy_deduction = view.findViewById(R.id.tv_energy_deduction);
        llAutoSb = view.findViewById(R.id.llAutoSb);
        tv_chapter_pay = view.findViewById(R.id.tv_chapter_pay);
        tv_ResouForMe_pay = view.findViewById(R.id.tv_ResouForMe_pay);
        tv_chapter_batch_pay = view.findViewById(R.id.tv_chapter_batch_pay);

    }

    /**
     * 设置横线颜色
     *
     * @param color
     * @return
     */
    public PayChapterView setLineColor(int color) {
        this.viewLine.setBackgroundColor(color);
        return this;
    }

    /**
     * 设置价格
     *
     * @param price
     * @return
     */
    public PayChapterView setPayPrice(String price) {
        this.price_text.setText(price);
        return this;
    }

    /**
     * 设置余额
     *
     * @param price
     * @return
     */
    public PayChapterView setBlance(Integer price, Integer price2) {
        String value = "";
        String value2 = "";
        if (price > 0) {
            value = price + "热币";
        } else {
            value = "0热币";
        }
        if (price2 > 0) {
            value2 = "+" + price2 + "热券";
        }
        this.balance.setText(value + value2);
        return this;
    }

    /**
     * 设置实际支付金额
     *
     * @param realPayPrice
     * @return
     */
    public PayChapterView setRealPayPrice(String realPayPrice) {
        this.tv_really_price.setText(realPayPrice);
        return this;
    }

    /**
     * 设置实际支付,___后面面描述
     *
     * @param des
     * @return
     */
    public PayChapterView setRealPayDes(String des) {
        this.tv_energy_deduction.setText(des);
        return this;
    }

    /**
     * 设置原价
     *
     * @param des
     * @return
     */
    public PayChapterView setPayYPrice(String des) {
        this.tvYPrice.setText(des);
        this.tvYPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //删除线
        this.tvYPrice.getPaint().setAntiAlias(true);// 抗锯齿
        return this;
    }

    /**
     * 设置原价的控件实例
     *
     * @return
     */
    public TextView getPayYPriceView() {
        return this.tvYPrice;
    }

    /**
     * 设置支付金额,___后面面描述
     *
     * @param des
     * @return
     */
    public PayChapterView setYDes(String des) {
        this.tvDes.setText(des);
        return this;
    }

    /**
     * 活动能量值
     *
     * @return
     */
    public TextView getActiveText() {
        return tv_energy_deduction;
    }

    /**
     * 获取是否自动购买
     *
     * @return
     */
    public boolean isAutoPay() {
        return isAutoPay;
    }

    /**
     * 购买按钮
     *
     * @return
     */
    public TextView getChapterPay() {
        return tv_chapter_pay;
    }

    /**
     * 热搜为我买单
     *
     * @return
     */
    public TextView getChapterResouForMePay() {
        return tv_ResouForMe_pay;
    }

    /**
     * 活动
     *
     * @return
     */
    public LinearLayout getChapterActive() {
        return ll_really_price_container;
    }

    /**
     * Vip包月
     *
     * @return
     */
    public TextView getChapterVipSubMonthly() {
        return tvMonthlySub;
    }

    /**
     * 自动购买按钮
     *
     * @return
     */
    public LinearLayout getChapterAutoPay() {
        return llAutoSb;
    }

    /**
     * 更多书籍
     *
     * @return
     */
    public TextView getChapterMoreBook() {
        return tv_chapter_batch_pay;
    }


    /***
     * 显示容器
     * @return
     */
    public LinearLayout getShowContent() {
        return llShowContent;
    }

    /**
     * 设置是否购买
     *
     * @param autoPay
     * @return
     */
    public PayChapterView setAutoPay(boolean autoPay) {
        isAutoPay = autoPay;
        if (isAutoPay) {
            ivAutoSb.setImageResource(R.mipmap.yd_dy_xz);
        } else {
            ivAutoSb.setImageResource(R.mipmap.yd_dy);
        }
        Log.i("坐标", "setAutoPay: " + isAutoPay);
        return this;
    }

    /**
     * 获取是是否全本购买
     *
     * @return
     */
    public boolean isAllBookPay() {
        return isAllBookPay;
    }

    public boolean isCliable() {
        return isCliable;
    }

    public PayChapterView setCliable(boolean cliable) {
        this.isCliable = cliable;
        return this;
    }

    /**
     * 设置是否全本购买
     *
     * @param allBookPay
     */
    public void setAllBookPay(boolean allBookPay) {
        isAllBookPay = allBookPay;
        if (isAllBookPay) {
//            tv_chapter_pay.setText("全本购买");
            tv_chapter_batch_pay.setVisibility(View.INVISIBLE);
//            ll_really_price_container.setVisibility(View.INVISIBLE);
            llAutoSb.setVisibility(View.INVISIBLE);
        } else {
//          tv_chapter_pay.setText("购买");
            tv_chapter_batch_pay.setVisibility(View.VISIBLE);
//            ll_really_price_container.setVisibility(View.VISIBLE);
            llAutoSb.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置支付的背景颜色
     *
     * @param color
     * @return
     */
    public PayChapterView setPayBackgroundColors(int color) {
        this.llContent.setBackgroundColor(color);
        return this;
    }

    /**
     * 设置购买的文案
     *
     * @param payText
     * @return
     */
    public PayChapterView setPayText(String payText) {
        tv_chapter_pay.setText(payText);
        return this;
    }

    /**
     * 设置字体颜色
     *
     * @param color
     * @return
     */
    public PayChapterView setTextColor(int color) {
        price_text_title.setTextColor(color);
        tvSPay.setTextColor(color);
        balance_title.setTextColor(color);
        balance.setTextColor(color);
        return this;
    }
}
